package com.atguigu.url.util;

import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * @version V1.0
 * @author: guangcheng
 * @date: 2021/11/17 14:58
 * @description:
 */
@Component
public class TaskSplitUtil {


    /**
     * 拆集合
     * 拆分之后的size: 要么是1  要么是piece
     *
     * @param urlList a
     * @param piece   拆多少份?
     * @return
     */
    public ArrayList<ArrayList<String>> listSplitTo2List(ArrayList<String> urlList, int piece) {
        System.out.println(" 执行 ==> listSplitTo2List(ArrayList<String> urlList, int piece) ");

        ArrayList<ArrayList<String>> listListList = new ArrayList<>();

        //计算每段大小
        int size = urlList.size();
        int n1 = size / piece;
        System.out.println("段大小n1 = " + n1);

        //组合数据
        for (int i = 1; i <= piece; i++) {

            if (i < piece) {

                ArrayList<String> list = new ArrayList<>();
                for (int i1 = (i - 1) * n1; i1 < i * n1; i1++) {
                    list.add(urlList.get(i1));
                }

                listListList.add(list);
            } else if (i == piece) {

                ArrayList<String> list = new ArrayList<>();
                for (int i1 = (piece - 1) * n1; i1 < urlList.size(); i1++) {
                    list.add(urlList.get(i1));
                }

                listListList.add(list);
            }

        }

        return listListList;
    }
}

